﻿/***************************************************************************************/
// Author                 - Balaji Venkatesan
// Student ID             - 12527079
// Version                - 1.0
// CodeName               - Northodyne Solutions
// Module                 - E-Business Technologies
// Date                   - 12-September-2011
/***************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Login : System.Web.UI.Page
{

    //public bool loginflag = false;
    public string _connStr = "Initial Catalog=DBNorthodyneSolutions; Data Source=ABC\\SQLExpress;Trusted_Connection=Yes"; 

    protected void Page_Load(object sender, EventArgs e)
    {
        Label lblwelcome = (Label)Master.FindControl("lblWelcome");
        Label lblusername = (Label)Master.FindControl("lblUserName");
        Label lblexpression = (Label)Master.FindControl("lblExpression");
        HyperLink hyplogin = (HyperLink)Master.FindControl("hyplogin");
        lblwelcome.Visible = false;
        lblusername.Visible = false;
        lblexpression.Visible = false;
        hyplogin.Visible = false;

        if (!IsPostBack)
        {
            ddlLoginType.Items.Add("Administrator");
            ddlLoginType.Items.Add("Customer");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {

        Session["UserName"] = txtUserName.Text;

         using (SqlConnection connection = new SqlConnection(_connStr))
         {
            connection.Open();
            string selectquery = "SELECT UserName, Password from Tb_Login WHERE UserType=@UserType";

            using (SqlCommand cmdSelectLogin = new SqlCommand(selectquery, connection))
            {
                SqlParameter[] selectPrms = new SqlParameter[1];
                selectPrms[0] = new SqlParameter("@UserType", SqlDbType.NVarChar, 50);
                selectPrms[0].SqlValue = ddlLoginType.SelectedItem.Text;

                cmdSelectLogin.Parameters.AddRange(selectPrms);

                using (SqlDataReader readerLogin = cmdSelectLogin.ExecuteReader())
                {
                    while (readerLogin.Read())
                    {

                        if (txtUserName.Text == readerLogin["UserName"].ToString() && txtPassword.Text == readerLogin["Password"].ToString())
                        {
                            if (ddlLoginType.SelectedItem.Text == "Customer")
                                Response.Redirect("CustomerPropertyInformation.aspx");
                            else if (ddlLoginType.SelectedItem.Text == "Administrator")
                                Response.Redirect("AdminViewProperty.aspx");
                        }
                    }
                }
            }
            connection.Close();
         }
            
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("CustomerRegistration.aspx");
    }
    protected void lnkbtnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
}